<template>
  <div class="app-container">
    <div class="form">
      <div class="form-header"><strong>商户门店信息</strong></div>
      <el-form ref="dataForm1" :model="dataForm" :rules="rules" status-icon label-position="left" label-width="85px">
        <el-form-item label="门店名称" prop="shopName">
          <el-input v-model="dataForm.shopName" type="text" placeholder="门店名称"/>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contact">
          <el-input v-model="dataForm.contact" type="text" auto-complete="off" placeholder="联系人姓名"/>
        </el-form-item>
        <el-form-item label="联系人号码" prop="telephone">
          <el-input v-model="dataForm.telephone" type="text" auto-complete="off" placeholder="联系人号码"/>
        </el-form-item>
        <el-form-item label="门店区域">
          <el-cascader
            :options="options"
            v-model="selectedOptions"
            size="large"
            @change="handleChange" />
        </el-form-item>
        <el-form-item label="门店地址" prop="address">
          <el-input v-model="dataForm.address" type="text" auto-complete="off" placeholder="门店地址"/>
        </el-form-item>
      </el-form>
      <div class="form-header"><strong>法人信息</strong></div>
      <el-form ref="dataForm2" :model="dataForm" status-icon label-position="left" label-width="85px">
        <el-form-item label="法人姓名" prop="corporation">
          <el-input v-model="dataForm.corporation" type="text" placeholder="法人姓名"/>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idCard">
          <el-input v-model="dataForm.idCard" type="text" placeholder="身份证号码"/>
        </el-form-item>
      </el-form>
      <div style="margin-left:100px;">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { updateShopInfo, queryByShopId } from '@/api/shop'
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
import { MessageBox } from 'element-ui'
export default {
  name: 'ShopInfo',
  data() {
    return {
      options: regionData,
      selectedOptions: [],
      shopId: '',
      dataForm: {
      },
      rules: {
        shopName: [{ required: true, message: '请输入商户名称', trigger: 'blur' }],
        contact: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }],
        telephone: [{ required: true, message: '请输入联系人号码', trigger: 'blur' }]
      }
    }
  },
  created() {
    const activeCode = this.$route.query.activeCode
    queryByShopId(activeCode).then(resp => {
      if (resp.data.code === '310') {
        MessageBox.alert(resp.data.message, '错误', {
          confirmButtonText: '确定',
          type: 'error'
        }).then(() => {
          this.$router.push({
            path: '/'
          })
        })
      } else {
        this.dataForm = resp.data.data
        const city = resp.data.data.city
        const province = resp.data.data.province
        const region = resp.data.data.region
        this.selectedOptions = [TextToCode[province].code, TextToCode[province][city].code, TextToCode[province][city][region].code]
      }
    })
  },
  methods: {
    cancel() {
    },
    submit() {
      this.$refs['dataForm1'].validate((valid) => {
        if (valid) {
          console.log(valid)
          updateShopInfo(this.dataForm).then(resp => {
            this.$router.push({
              path: '/active-success'
            })
          })
        } else {
          return false
        }
      })
    },
    handleChange(value) {
      this.dataForm.province = CodeToText[value[0]]
      this.dataForm.city = CodeToText[value[1]]
      this.dataForm.region = CodeToText[value[2]]
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
  background-color: #fefefe;
  .form{
    margin: 10px auto;
    width: 100%;
    min-width: 320px;
    max-width: 480px;
    .form-header{
      text-align: center;
      color: #666;
      font-size: 28px;
      padding: 5px 0;
      margin: 10px 0 20px;
    }
  }
}
</style>
